package com.zhijiang.hospital.service.impl;

import com.zhijiang.hospital.entity.Permission.Menu;
import com.zhijiang.hospital.entity.Permission.Meta;
import com.zhijiang.hospital.entity.SysUser;
import com.zhijiang.hospital.entity.dto.EmployeeDto;
import com.zhijiang.hospital.entity.dto.HisUserDto;
import com.zhijiang.hospital.mapper.ComEmployeeMapper;
import com.zhijiang.hospital.mapper.EmployeeDtoMapper;
import com.zhijiang.hospital.mapper.MenuMapper;
import com.zhijiang.hospital.mapper.MetaMapper;
import com.zhijiang.hospital.mapper.RoleMapper;
import com.zhijiang.hospital.mapper.SysRoleMenuMapper;
import com.zhijiang.hospital.mapper.SysUserRoleMapper;
import com.zhijiang.hospital.mapper.UserMapper;
import com.zhijiang.hospital.service.UserService;
import com.zhijiang.util.HttpPost;
import com.zhijiang.util.jwt.JwtTokenUtils;
import com.zhijiang.util.redis.RedisCache;
import com.zhijiang.util.redis.RedisConstants;
import com.zhijiang.util.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * fan
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    //@Autowired
    //AuthenticationManager authenticationManager;
    //@Autowired
    //RedisCache redisCache;
    //@Autowired
    //GlobalSecurityConfig securityConfig;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    ComEmployeeMapper comEmployeeMapper;
    @Autowired
    RedisService redisService;
    @Autowired
    RedisCache redisCache;
    @Autowired
    EmployeeDtoMapper employeeDtoMapper;
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    MetaMapper metaMapper;


    @Override
    public HashMap<String, String> login(SysUser sysUser) {
        System.out.println("");
        HisUserDto hisUserDto = HttpPost.validateUser(sysUser);
        if (!hisUserDto.getCheckUserResult()) {
            throw new RuntimeException("账号密码有误");
        } else {
            EmployeeDto oneEmployeeDto = employeeDtoMapper.findOneEmployeeDto(sysUser.getUsername());
            String token = JwtTokenUtils.getToken(oneEmployeeDto.getEmplCode(), "zhijiang");
            redisService.setStringKeyAndValue(RedisConstants.LOGIN + oneEmployeeDto.getEmplCode(), oneEmployeeDto, 30);
            //redisCache.setCacheObject(RedisConstants.LOGIN + oneEmployeeDto.getEmplCode(), oneEmployeeDto, RedisConstants.LOGIN_CODE_TTL, RedisConstants.LOGINUNIT);
            HashMap<String, String> map = new HashMap<>();
            map.put("token", token);
            return map;

        }
    }

    @Override
    public void loginOut() {

    }

    @Override
    public void updateUser(SysUser sysUser) {

    }

    @Override
    public boolean updatePassword(String username, String password, String userId) {
        return false;
    }

    @Override
    public Object findLimitsOfAuthorityList(String emplCode) {
        List<Menu> menu = menuMapper.loadMenu(emplCode);
        List<Meta> meta = metaMapper.findMeta();
        List<Meta> list = new ArrayList<>();
        for (Menu menu1 : menu) {
            for (Meta meta1 : meta) {
                if (menu1.getMeta().equals(meta1.getMetaId())) {
                    list.add(meta1);
                }
            }
            menu1.setMetas(list);
        }
        return menu;
    }

    @Override
    public boolean findAuthority(String emplCode) {
        List<EmployeeDto> middleLevelLeaders = employeeDtoMapper.findMiddleLevelLeaders();
        List<String> collect = middleLevelLeaders.stream().map(EmployeeDto::getEmplCode).collect(Collectors.toList());
        if (collect.contains(emplCode)) {
            return true;
        } else {
            return false;
        }
    }


}




